<?php
/**
 * Controller is the customized base controller class.
 * All controller classes for this application should extend from this base class.
 */
class Controller extends CController
{
	/**
	 * @var string the default layout for the controller view. Defaults to '//layouts/column1',
	 * meaning using a single column layout. See 'protected/views/layouts/column1.php'.
	 */
	public $layout='//layouts/column1';
	
	public $startTime;
	public $endTime;
	
	
	public $urlID;
	/**
	 * @var array context menu items. This property will be assigned to {@link CMenu::items}.
	 */
	public $menu=array();
	/**
	 * @var array the breadcrumbs of the current page. The value of this property will
	 * be assigned to {@link CBreadcrumbs::links}. Please refer to {@link CBreadcrumbs::links}
	 * for more details on how to specify this property.
	 */
	public $breadcrumbs=array();
	
	public $theme;
	
	public $cs = null;
	
	public $pageDescription = null;
	public $pageKeyWords = null;
	public $pageRobots = 'index';
	
	public $meta = array();
	

	public function __construct($id,$module=null)
	{
		parent::__construct($id,$module);
        
        
		
//// 		$dd = new CClientScript();
//		
//		if($this->pageKeyWords == null)
//			Yii::app()->clientScript->registerMetaTag(Settings::model()->getKeyword(), 'KeyWords');
//		else 
//			Yii::app()->clientScript->registerMetaTag($this->pageKeyWords, 'KeyWords');
//		
//		if ($this->pageDescription == Settings::model()->getDescription())
//			Yii::app()->clientScript->registerMetaTag(Settings::model()->getDescription(), 'Description');
//		else 
//			Yii::app()->clientScript->registerMetaTag($this->pageDescription, 'Description');
//
//
//		Yii::app()->clientScript->registerMetaTag($this->pageRobots, 'Robots');
//		
//		
//		Yii::app()->clientScript->registerMetaTag(Settings::model()->getEditor(), 'Generator');
//		Yii::app()->clientScript->registerMetaTag(Settings::model()->getAuthor(), 'Author');
		
		
		$this->meta = array(
		    'Description' => Settings::model()->getDescription(),
		    'KeyWords' => Settings::model()->getKeyword(),
		    'Robots' => 'all',
		    'Generator' => 'Eclipse 3.7',
		    'Author' => '冷月十三',
		);
		
//		$this->setMetaInfo($this->meta);
		
//		Yii::app()->name = Settings::model()->getItemByKey('siteName')->set_value;
		Yii::app()->params['email'] = Settings::model()->getItemByKey('email')->set_value;
		
		Yii::setPathOfAlias('theme',$this->theme->baseUrl);
        
        //设置Common目录
        
        $path = dirname(dirname(Yii::getPathOfAlias('application'))).'\yuekegu-common';

        Yii::setPathOfAlias('common',$path);
        
	}
	
	/**
	 * $item = array(
	 * "KeyWords" => '',
	 * "Description" => '',
	 * );
	 * Enter description here ...
	 * @param unknown_type $item
	 */
	
	public function setMetaInfo()
	{
		foreach ($this->meta as $key => $value)
		{
			Yii::app()->clientScript->registerMetaTag($value, $key);
		}
	}
	
	
	protected function beforeAction($action)
	{
//		$this->startTime = microtime();
		
		$this->urlID = ucfirst($this->module->id).ucfirst($this->id).ucfirst($this->action->id);	
		
// 		echo $this->urlID;
		
		$auth = Yii::app()->authManager;
		
		if (!$auth->getAuthItem($this->urlID))
			$auth->createOperation($this->urlID);
		
// 		echo Yii::app()->user->id;
		
		if (!Yii::app()->user->isGuest)
		{
			if (!$auth->checkAccess($this->urlID,Yii::app()->user->id))
			{
//				echo "OK";
				throw new CHttpException(403,'你无权访问该页.');
			} 			
		}
		
		return true;
	}
	
	
	protected function afterAction($action)
	{
//		$this->endTime = microtime();
//		
//		$time = $this->endTime - $this->startTime;
//		
//		UtilTools::writeToFile($this->id.$action->id.$time);
//		UtilTools::writeToFile($_GET);
//		UtilTools::writeToFile($_POST);
		
		
	}
	
	/**
	 * 获取URLID
	 */
	public function getUrlID()
	{
		return $this->urlID;
	}

}